/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import org.synyx.hades.domain.AbstractPersistable;

import javax.persistence.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({
	@NamedQuery(name = "Configuration.findByAll", query = "from Configuration o"),
	@NamedQuery(name = "Configuration.findByProduct", query = "select distinct o from Configuration o, in (o.configRevisions) c where c.version.product = ?"),
	@NamedQuery(name = "Configuration.findByProductCount", query = "select count(distinct o) from Configuration o, in (o.configRevisions) c where c.version.product = ?")})
public class Configuration extends AbstractPersistable<Long> {
	@Column(unique = true)
	private String name;
	private String description;

	@OneToMany(mappedBy="configuration")
	private List<ConfigRevision> configRevisions;

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<ConfigRevision> getConfigRevisions() {
		return configRevisions;
	}

	public void setConfigRevisions(List<ConfigRevision> configRevisions) {
		this.configRevisions = configRevisions;
	}

	public ConfigRevision getLatestConfigRevision() {
		Collections.sort(configRevisions, new Comparator<ConfigRevision>() {
			@Override
			public int compare(ConfigRevision o1, ConfigRevision o2) {
				if (o1.getNr() > o2.getNr()) {
					return -1;
				} else if (o1.getNr() < o2.getNr()) {
					return 1;
				}
				return 0;
			}
		});
		return configRevisions.get(0);
	}
	
	@Override
	public String toString() {
		return String.format(
				"Configuration [description=%s, name=%s, getId()=%s]",
				description, name, getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result
				+ ((configRevisions == null) ? 0 : configRevisions.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof Configuration)) {
			return false;
		}
		Configuration other = (Configuration) obj;
		if (description == null) {
			if (other.description != null) {
				return false;
			}
		} else if (!description.equals(other.description)) {
			return false;
		}
		if (configRevisions == null) {
			if (other.configRevisions != null) {
				return false;
			}
		} else if (!configRevisions.equals(other.configRevisions)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}
}
